<?php

use app\models\AsrAudio;
use app\models\AsrAudioClassify;
use function foo\func;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $searchModel app\models\search\AsrAudioSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="asr-audio-index">
    <p>
        <?= Html::a('新增流程', ['create'], ['class' => 'btn  btn-default btn-lg fa fa-plus']) ?>
    </p>
    <div class="box">
        <div class="box-header">
            <h3 class="box-title">语音流程列表</h3>
        </div>
        <!-- /.box-header -->
        <div class="box-body">
            <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            ['attribute'=>'name',
                'footer' => '
                    <input id="num" class="btn btn-default btn-xs btn-delete" placeholder="每页显示的条数" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'\')}else{this.value=this.value.replace(/\D/g,\'\')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'\')}else{this.value=this.value.replace(/\D/g,\'\')}">
                ',
            ],
            ['attribute' => 'create_at',
                'value'=>
                    function($model){
                        return  date('Y-m-d H:i:s',$model->create_at);
                    },
            ],
            ['attribute' => 'states',
                'content'=>
                    function($model){
                        $states = $model->states;
                        if($states==0){
                            return '<span style="color:#9b34da;"><b>正在审核</b></span>';
                        }elseif ($states==1){
                            return '<span style="color:#00A65A;"><b>审核通过</b></span>';
                        }elseif ($states==2){
                            return '<span style="color:#ff2c2f;"><b>审核不通过</b></span>';
                        }else{
                            return "无状态";
                        }
                    },
            ],
            ['attribute' => 'audio_classify_id',
                'value'=>
                    function($model){
                        return  $model->getAudioClassify()->one()->name;
                    },
            ],

            ['class' => 'yii\grid\ActionColumn',//下面是新增加的按按钮
                'header'=>'操作',
                'template' => '{detail} &nbsp;&nbsp;{update}&nbsp;&nbsp;&nbsp;{delete} ',
                'buttons' => [
                    'detail'=> function ($url, $model, $key) use($audioClassify) {



                        return '
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal'.$key.'">
 重命名
</button>
<!-- Modal -->
<div class="modal fade" id="myModal'.$key.'" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">重命名</h4>
      </div>
      <div class="modal-body">
            <div class="form-group">
                <label for="inputEmail3" class=" control-label" style="margin-top:4px;">流程名称：</label>
                <div class="">
                    <input id="name'.$key.'" autocomplete="off" name="name'.$key.'" value='.$model->name.' type="text" class="form-control"  placeholder="请填写流程名称">
                </div>
                    <input type="hidden" id='.$key.' value='.$key.'>
                <label for="inputEmail3" class=" control-label" >流程种类：</label>
                <div class="" >
                    '. Html::dropDownList("selected",$model->audio_classify_id,ArrayHelper::map($audioClassify,'audio_classify_id','name'),["value"=>$model->audio_classify_id,"class"=>"form-control" ,"id"=>'selected'.$key]).'
                </div>
            </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary" id="'.$key.'" onclick="fun('.$key.')">保存</button>
      </div>
    </div>
  </div>
</div>
                                ';
                    },

                    'update'=> function ($url, $model, $key) {
                        $url = 'update?id='.$key.'&page='.(isset($_GET['page'])?$_GET['page']:0).'&per-page='.(isset($_GET['per-page'])?$_GET['per-page']:0);
                        $result = Html::a('<span class="btn btn-success fa fa-sticky-note-o">修改</span>&nbsp;', $url,
                            ['id' => 'update',
                                'title' => '修改'] );
                        return $result;
                    },
                    'delete' => function ( $url,$model) {
                        return  '<a href="'.$url.'" title="删除" aria-label="删除" data-pjax="0" data-confirm="您确定要删除此项吗？" data-method="post"><span class="btn btn-danger fa fa-remove">删除</span></a>';
                    },
                ],
                'options'=>[
                    'width'=>'20%',
                ],
                'footerOptions' => ['colspan' => 1],  //设置删除按钮垮列显示；
            ]
        ],
        'filterModel' => $searchModel,
        'showFooter' => true,  //设置显示最下面的footer
        'pager'=>[
            'firstPageLabel'=>"第一页",
            'lastPageLabel'=>'最后一页',
            'options'=>[
                'class'=>'pagination pagination-sm no-margin pull-right'
            ],

        ],
        'tableOptions'=>[
                'class'=>'table table-hover'
        ]
    ]); ?>
        </div>
    </div>
</div>

<script type="text/javascript">
    var urls = 'newname';
    function fun($key) {
        $.ajax({
            type: 'post',
            url: urls,
            data: {name: $('#name'+$key).val(), audio_classify_id: $('#selected'+$key).val(), id: $key},
            success: function (data) {
                if (data == 1) {
                    alert('修改成功');
                    window.location.reload();
                } else {
                    alert('修改失败');
                }
            }
        });
    }
    $('#num').blur(function(){//当鼠标失去焦点是触发事件,实现用户自定义分页
        var url = "<?= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] ?>";
        $.ajax({
            type:'post',
            async:true,
            url:url,
            data:{pageSize:$('#num').val()},
            success:function(data){
                window.location.reload();
            },
            error:function(error){

            }
        });
    });
    $('#asrRes').click(function(){
        $(this).attr('hidden','hidden');
    });
</script>